package com.zhuss.core.processor;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.information.UpdateInformationBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.InformationDao;
import com.zhuss.core.db.entity.Information;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * @description: 通知公告修改
 * @author: zhushanshuo
 * @create: Tue Feb 02 15:16:28 CST 2021
 **/
@Component("informationUpdate")
public class InformationUpdate extends AbstractProcessor<Object, UpdateInformationBo> implements Processor<Object, UpdateInformationBo> {

    @Autowired
    private InformationDao informationDao;

    @Autowired
	@Qualifier("informationRead")
	private InformationRead informationRead;

    /**
     * 校验
     */
    @Override
    public void check(UpdateInformationBo bo) throws DzcxException {
    	// TODO 修改 通知公告 校验， 请补充

    	Long informationId = bo.getInformationId();
    	String name = bo.getTitle();
    	if (informationId == null || informationId <= 0l) {
    		throw new DzcxException(ExceptionCode.通知公告ID为空);
    	}
    	if (name == null || "".equals(name.trim())) {
    		 throw new DzcxException(ExceptionCode.标题为空);
    	}

    	List<Information> list = informationRead.listNotId(name, informationId);
    	if (!list.isEmpty()) {
    		 throw new DzcxException(ExceptionCode.标题已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateInformationBo param) {
    	Information record = new Information();
    	record.setInformationId(param.getInformationId());
    	record.setType(param.getType());
    	record.setTitle(param.getTitle());
    	record.setContent(param.getContent());
    	record.setStatus(param.getStatus());


    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	informationDao.updateByPrimaryKeySelective(record);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateInformationBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.通知公告修改.key());
    	record.setTargetId(param.getInformationId());
    	record.setTargetName(param.getTitle());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
